clear all
set more off

cd           "/CGR_credit"
local out2 = "/CGR_credit/outregsQJE/"
use "step001_credit_QJE.dta"

tsset firm_id time

*** define treatment as given inflation TIMES treatment status
gen Treat=treatment_old*infl


* multiply by 100 mean utilization rate
gen    util_ratio_mean0 = util_ratio_mean * 100
drop   util_ratio_mean
rename util_ratio_mean0 util_ratio_mean

* rename variable and create first difference
rename util_ratio_mean ur_crl  /*Utilization Rate of Credit Lines */
gen dur_crl=d.ur_crl

*** winsorize utilizatior rate and delta(utilization rate)
winsor ur_crl, gen(ur_crl_w) p(0.02)
winsor dur_crl, gen(dur_crl_w) p(0.02)

*** winsorize firm expected price change
winsor dprez, gen(dprez_w) p(0.02)

local controls0=" l2.dprez_w  i.l2.sitimp5 i.l2.occtot i.l2.sitgen i.l2.promig i.l2.liquid"	

***======================================================
*** 					IRF: cumulative [from levels]
***						No firm FE
***======================================================
keep if time>=208

*** horizon 1 ******************************************************************
ivreghdfe dur_crl_w `controls0' ///
	(l.it12_all = l.Treat) [aw=peso] , ///
	ffirst dkraay(2) absorb(i.setton5#i.trime )
	
	       outreg2 using "`out2'Table03_PanelD.dta", replace dec(3)  ///
	addstat( "1st stage F stat", e(widstat))	
	
	capture drop smpl1
	gen smpl1=1 if e(sample)
	
ivreghdfe l.it12_all  l.Treat `controls0' ///
	 [aw=peso] if smpl1==1, ///
	 dkraay(2) absorb(i.setton5#i.trime )
        outreg2 using "`out2'Table03_PanelD_first_stage.dta", replace dec(3)  ///
		
*** horizon 2 ******************************************************************

gen FC1=f.ur_crl-l.ur_crl
winsor FC1, gen(FC1_w2) p(0.02)
ivreghdfe FC1_w2 `controls0' ///
	(l.it12_all = l.Treat) [aw=peso] , ///
	dkraay(2) absorb(i.setton5#i.trime)
	outreg2 using "`out2'Table03_PanelD.dta", append dec(3)  ///
	addstat( "1st stage F stat", e(widstat))	
	
	capture drop smpl1
	gen smpl1=1 if e(sample)
	
ivreghdfe l.it12_all  l.Treat `controls0' ///
	 [aw=peso] if smpl1==1, ///
	 dkraay(2) absorb(i.setton5#i.trime )
        outreg2 using "`out2'Table03_PanelD_first_stage.dta", append dec(3)  ///
	

	
***	horizon 3-6
forvalues jj = 2(1)5 {
gen FC`jj'=f`jj'.ur_crl-l.ur_crl
winsor FC`jj', gen(FC`jj'_w2) p(0.02)
ivreghdfe FC`jj'_w2 `controls0' ///
	(l.it12_all = l.Treat) [aw=peso] , ///
	dkraay(2) absorb(i.setton5#i.trime)
	outreg2 using "`out2'Table03_PanelD.dta", append dec(3) ///
	addstat( "1st stage F stat", e(widstat))	
	
	capture drop smpl1
	gen smpl1=1 if e(sample)
	
ivreghdfe l.it12_all  l.Treat `controls0' ///
	 [aw=peso] if smpl1==1, ///
	 dkraay(2) absorb(i.setton5#i.trime )
        outreg2 using "`out2'Table03_PanelD_first_stage.dta", append dec(3)  ///
	
	
	
}
